using System;

using OpenXmlWriter.Packaging;

namespace OpenXmlWriter.Packaging
{
	public class CoreFilePropertiesPart : OpenXmlPart
	{
      private const string PartRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/metadata/core-properties";

      private const string PartName = "core";
      private const string PartPath = "docProps";
      private const string PartExtension = ".xml";
      public const string PartContentType = "application/vnd.openxmlformats-package.core-properties+xml";
      private string _targetPath;

      public override void Initialize(OpenXmlPackage openXmlPackage, OpenXmlPartContainer openXmlPartContainer)
      {
        base.Initialize (openXmlPackage, openXmlPartContainer);
        _targetPath = CreateUniquePath();
        _contentType = new ContentType(_targetPath, PartContentType, false);
      }

      private string CreateUniquePath()
      {
        return PartPath + "/" + PartName + PartExtension;
      }

      public override string RelationshipType
      {
        get
        {
          return PartRelationshipType;
        }
      }

      public override ContentType ContentType
      {
        get
        {
          return _contentType;
        }
      }

      public override void AddTo(OpenXmlPartCollection parts)
      {
        if (parts.ContainsKey(_targetPath))
        {
          throw new InvalidOperationException();
        }

        parts.Add(_targetPath, this);
      }

      public override string TargetPath
      {
        get
        {
          return _targetPath;
        }
      }  
    }
  }